package com.wyx.springbootspringkafkademo.config;

import io.confluent.kafka.schemaregistry.client.SchemaRegistryClient;
import io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient;
import io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException;
import io.confluent.kafka.schemaregistry.avro.AvroSchema;

import java.io.IOException;

public class SchemaRegistry{
    public static void main(String[] args) {
        String schemaRegistryUrl = "http://localhost:8081";
        String subject = "user-topic";
        String schemaJson = "{\n" + "  \"type\": \"record\",\n" + "  \"name\": \"User\",\n"
            + "  \"namespace\": \"com.wyx.springbootspringkafkademo.entity\",\n" + "  \"fields\": [\n"
            + "    {\"name\": \"id\", \"type\": \"int\"},\n" + "    {\"name\": \"name\", \"type\": \"string\"},\n"
            + "    {\"name\": \"email\", \"type\": \"string\"}\n" + "  ]\n" + "}";

            SchemaRegistryClient client = new CachedSchemaRegistryClient(schemaRegistryUrl, 100);

            try {
                int schemaId = client.register(subject, new AvroSchema(schemaJson));
                System.out.println("Schema registered with ID: " + schemaId);
            } catch (RestClientException e) {
                e.printStackTrace();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
    }
    }